package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.domain.SysCompanies;
import com.ruoyi.system.mapper.SysCompaniesMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysDeviceEarlyWarningMapper;
import com.ruoyi.system.domain.SysDeviceEarlyWarning;
import com.ruoyi.system.service.ISysDeviceEarlyWarningService;
import com.ruoyi.common.core.text.Convert;

import javax.validation.Validator;

/**
 * 设备预警单Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-07-21
 */
@Service
public class SysDeviceEarlyWarningServiceImpl implements ISysDeviceEarlyWarningService {

    private static final Logger log = LoggerFactory.getLogger(SysDeviceEarlyWarningServiceImpl.class);

    @Autowired
    private SysDeviceEarlyWarningMapper sysDeviceEarlyWarningMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询设备预警单
     * 
     * @param dewId 设备预警单主键
     * @return 设备预警单
     */
    @Override
    public SysDeviceEarlyWarning selectSysDeviceEarlyWarningByDewId(Long dewId)
    {
        return sysDeviceEarlyWarningMapper.selectSysDeviceEarlyWarningByDewId(dewId);
    }

    /**
     * 查询设备预警单列表
     * 
     * @param sysDeviceEarlyWarning 设备预警单
     * @return 设备预警单
     */
    @Override
    public List<SysDeviceEarlyWarning> selectSysDeviceEarlyWarningList(SysDeviceEarlyWarning sysDeviceEarlyWarning)
    {
        return sysDeviceEarlyWarningMapper.selectSysDeviceEarlyWarningList(sysDeviceEarlyWarning);
    }

    /**
     * 新增设备预警单
     * 
     * @param sysDeviceEarlyWarning 设备预警单
     * @return 结果
     */
    @Override
    public int insertSysDeviceEarlyWarning(SysDeviceEarlyWarning sysDeviceEarlyWarning)
    {
        sysDeviceEarlyWarning.setCreateTime(DateUtils.getNowDate());
        return sysDeviceEarlyWarningMapper.insertSysDeviceEarlyWarning(sysDeviceEarlyWarning);
    }

    /**
     * 修改设备预警单
     * 
     * @param sysDeviceEarlyWarning 设备预警单
     * @return 结果
     */
    @Override
    public int updateSysDeviceEarlyWarning(SysDeviceEarlyWarning sysDeviceEarlyWarning)
    {
        sysDeviceEarlyWarning.setUpdateTime(DateUtils.getNowDate());
        return sysDeviceEarlyWarningMapper.updateSysDeviceEarlyWarning(sysDeviceEarlyWarning);
    }

    /**
     * 批量删除设备预警单
     * 
     * @param dewIds 需要删除的设备预警单主键
     * @return 结果
     */
    @Override
    public int deleteSysDeviceEarlyWarningByDewIds(String dewIds)
    {
        return sysDeviceEarlyWarningMapper.deleteSysDeviceEarlyWarningByDewIds(Convert.toStrArray(dewIds));
    }

    /**
     * 删除设备预警单信息
     * 
     * @param dewId 设备预警单主键
     * @return 结果
     */
    @Override
    public int deleteSysDeviceEarlyWarningByDewId(Long dewId)
    {
        return sysDeviceEarlyWarningMapper.deleteSysDeviceEarlyWarningByDewId(dewId);
    }

    @Override
    public String importDeviceEarlyWarning(List<SysDeviceEarlyWarning> list, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(list) || list.size() == 0) {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (SysDeviceEarlyWarning obj : list) {
            try {
                // 验证是否存在这个公司
                SysDeviceEarlyWarning c = sysDeviceEarlyWarningMapper.selectSysDeviceEarlyWarningByDewId(obj.getDewId());
                if (StringUtils.isNull(c)) {
                    BeanValidators.validateWithException(validator, obj);
                    obj.setCreateBy(operName);
                    this.insertSysDeviceEarlyWarning(obj);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、预警单 " + obj.getSubstationName() + " 导入成功");
                } else if (isUpdateSupport) {
                    BeanValidators.validateWithException(validator, obj);
                    obj.setUpdateBy(operName);
                    this.updateSysDeviceEarlyWarning(obj);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、预警单 " + obj.getSubstationName() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、预警单 " + obj.getSubstationName() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、预警单 " + obj.getSubstationName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
